<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;

class SystemConfig extends Base
{
    use SoftDeletes;
    //
    protected $guarded = [];

    /**
     * 获取系统配置列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
        return $this->makeQuery($request)->paginate($request->input('limit', 10));
    }

    /**
     * 查询系统配置
     * @param Request $request
     * @return mixed
     */
    private function makeQuery(Request $request): mixed
    {
        $query = $this->select('*');
        if ($request->has('name')) {
            $query->where('name', 'like', '%' . $request->input('name') . '%');
        }
        if ($request->has('type')) {
            $query->where('type', $request->input('type'));
        }
        if ($request->has('key')) {
            $query->where('key', $request->input('key'));
        }
        if ($request->has('description')) {
            $query->where('description', 'like', '%' . $request->input('description') . '%');
        }
        return $query;
    }
}
